﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Text;
using Android.Content;
using Android.Widget;
using Android.Views;
using Android.Media;
using LinkNote.Model;

namespace LinkNote.Core
{
    public delegate void DeleteClickDelegate<T>(object v, T data);

    public delegate void DownloadFinishDelegate(string path);

    public class CoreHelper
    {
        public static void DownLoadFile(AttachmentBlock data, DownloadFinishDelegate callback)
        {
            string fn = data.remoteId + "_" + data.name;
            string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData);
            if (!System.IO.Directory.Exists(path))
                System.IO.Directory.CreateDirectory(path);
            string fullpath = Path.Combine(path, fn);
            WebQuery.FileDownload(data.remoteId, "下载文件", fullpath, delegate(QueryResult result,
                                                                        AsyncCompletedEventArgs args)
            {
                if (args.Error == null)
                {
                    data.file = fullpath;
                    DBHelper.sqlitedb.InsertOrUpdate(data);
                    if (callback != null)
                        callback(fullpath);
                }
            });
        }
    }

    public class AudioPlayer
    {
        private Context context;
        private ViewGroup parent;
        private MediaPlayer player;

        public event DeleteClickDelegate<AttachmentBlock> DeleteClick;
        private AttachmentBlock data;
        private ImageButton btnClose;

        public AudioPlayer(Context context, ViewGroup parent, AttachmentBlock fileName)
        {
            this.context = context;
            this.parent = parent;
            this.data = fileName;

            LayoutInflater li = LayoutInflater.FromContext(context);
            View v = li.Inflate(Resource.Layout.audioplayer, parent, false);

            //Get references to the child controls
            Button btnPlay = (Button)v.FindViewById(Resource.Id.btnPlay);
            TextView txtName = (TextView)v.FindViewById(Resource.Id.txtName);
            btnClose = v.FindViewById<ImageButton>(Resource.Id.btnClose);
            btnClose.Click += (sender, e) =>
            {
                if(data != null)
                    DBHelper.sqlitedb.Delete(data);
                parent.RemoveView(v);
                DeleteClick(sender, data);
            };
            parent.AddView(v);

            btnPlay.Enabled = false;
            if (string.IsNullOrEmpty(data.file) || !System.IO.File.Exists(data.file))
                CoreHelper.DownLoadFile(data, delegate { btnPlay.Enabled = true; });
            else
                btnPlay.Enabled = true;

            player = new MediaPlayer();
            player.SetDataSource(data.file);
            player.PrepareAsync();
            player.Completion += delegate(object sender, EventArgs args)
                {
                    player.Reset();
                    player.SetDataSource(data.file);
                    player.PrepareAsync();
                    btnPlay.SetBackgroundResource(Resource.Drawable.play);
                };

            txtName.Text = System.IO.Path.GetFileName(data.file);
            btnPlay.Click += (s, e) =>
            {
                if (!player.IsPlaying && System.IO.File.Exists(data.file))
                {
                    player.Start();
                    btnPlay.SetBackgroundResource(Resource.Drawable.audio);
                }
                else
                {
                    player.Pause();
                    btnPlay.SetBackgroundResource(Resource.Drawable.play);
                }
            };
        }

        public bool Editable
        {
            get { return btnClose.Visibility == ViewStates.Visible; }
            set
            {
                btnClose.Visibility = value ? ViewStates.Visible : ViewStates.Invisible;
            }
        }
    }
}
